package com.axx.android.setting;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

import com.axx.android.LetterApp;
import com.axx.android.R;
import com.axx.android.base.BaseActivity;
import com.axx.android.common.BaseModel;
import com.axx.android.common.Constants;
import com.axx.android.utils.GsonUtils;
import com.axx.android.utils.HttpUtils;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FeedBackActivity extends BaseActivity {


    @Bind(R.id.btn_back)
    Button btnBack;
    @Bind(R.id.layout_toolbar)
    RelativeLayout layoutToolbar;
    @Bind(R.id.et_title)
    EditText etTitle;
    @Bind(R.id.et_content)
    EditText etContent;
    @Bind(R.id.btn_send)
    Button btnSend;
    @Bind(R.id.sp_type)
    Spinner spType;
    private List<FeedBackDic.CodeListBean> codeList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_feed_back;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        fetchDic();
    }

    private void fetchDic() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("type", "1");
        HttpUtils.post(Constants.Url.Dic.DIC_CODE, hashMap, new HttpUtils.SJCallBack() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                BaseModel<FeedBackDic> dicBaseModel = null;
                try {
                    dicBaseModel = GsonUtils.fromJson(result, new TypeToken<BaseModel<FeedBackDic>>() {
                    });
                    if (dicBaseModel.isSuccess()) {
                        initAdapter(dicBaseModel.data
                                .getCodeList());
                    } else {
                        showToast(dicBaseModel.message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    showErrorParse();
                }

            }

            @Override
            public void onError() {
                hideLoading();
                showErrorNet();
            }
        });
    }

    private void initAdapter(List<FeedBackDic.CodeListBean> codeList) {
        this.codeList = codeList;
        List<String> list = new ArrayList<>();
        for (FeedBackDic.CodeListBean codeListBean : codeList) {
            list.add(codeListBean.getCodeName());
        }
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.view_item_sp, list);
        spType.setAdapter(arrayAdapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_send)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etContent.getText().toString())) {
            showToast("请输入反馈内容");
            return;
        }

        HashMap<String, String> map = new HashMap<>();
        map.put("userId", LetterApp.authUser.getId());
        map.put("problem", etContent.getText().toString());
        map.put("problemType", codeList.get(spType.getSelectedItemPosition()).getCodeType() + "");
        showLoading("");

        HttpUtils.post(Constants.Url.User.USER_FEED_BACK, map, new HttpUtils.SJCallBack() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                BaseModel dicBaseModel = null;
                try {
                    dicBaseModel = GsonUtils.fromJson(result, new TypeToken<BaseModel>() {
                    });
                    if (dicBaseModel.isSuccess()) {
                        showToast("意见反馈成功");
                        finish();
                    } else {
                        showToast(dicBaseModel.message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    showErrorParse();
                }
            }

            @Override
            public void onError() {
                hideLoading();
                showErrorNet();
            }
        });

    }
}
